<template>
  <div class="header">
    <!-- logo -->
    <a href="/">
      <img
        class="header-logo"
        src="../../assets/logo.png"
        width="25px"
        height="25px"
        alt="logo"
      />
      <span class="header-title">梦学谷会员管理系统</span>
    </a>

    <!-- 下拉菜单 -->
    <el-dropdown trigger="hover" @command="handleCommand">
      <span class="el-dropdown-link">
        {{username}}<i class="el-icon-arrow-down el-icon--right"></i>
      </span>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item command="pass">修改密码</el-dropdown-item>
        <el-dropdown-item command="logout">退出登录</el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>

    <!-- 修改密码 -->
  <el-dialog title="修改密码" :visible.sync="dialogFormVisible" width="500px">
      <el-form :model="passForm"  ref="dialogForm" status-icon :rules="rules" style="width:400px;" label-width="100px">
        <el-form-item label="原密码" prop="oldPass">
          <el-input type="password" v-model="passForm.oldPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="pass">
          <el-input type="password" v-model="passForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="passForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitPass">确 定</el-button>
      </div>
  </el-dialog>


  </div>
</template>

<script>
import userApi from "../../api/user"
export default {
  name: "",
  data() {
    //校验原密码
    let validateOldPass = async (rule,value,callback) => {
      let res = await userApi.doVerifyPass(this.userId,value)
      if(res.data.flag) {
        callback()
      }else{
        callback(new Error(res.data.message))
      }
    };
    //确认密码校验
    let validatePass = (rule,value,callback) => {
      if(value == this.passForm.pass){
        callback()
      }else{
        callback(new Error("两次密码输入不一致"))
      }
    }
    return {
      username : this.$store.state.userInfo.name || "",
      dialogFormVisible:false,
      passForm : {
        oldPass : "",
        pass : "",
        checkPass : ""
      },
       userId : this.$store.state.userInfo.id,
        rules : {
        oldPass : [
          {required : true, message : "原密码不能为空", trigger: "blur"},
          { validator :  validateOldPass, trigger: "blur"}
        ],
        pass : [
          {required : true, message : "新密码不能为空", trigger: "blur"}
        ],
        checkPass : [
          {required : true, message : "确认密码不能为空", trigger: "blur"},
          { validator :  validatePass, trigger: "blur"}
        ]
      }

    };
  },
  methods : {
    handleCommand(val){
      if(val == "pass"){
        this.changePass()
      }else if(val == "logout"){
        this.logout()
      }
      
    },
    //修改密码方法
    changePass(){
       this.dialogFormVisible = true;
    },
    //提交修改密码
    submitPass(){
      this.$refs['dialogForm'].validate(async valid => {
        if(valid){
          let res = await userApi.doChangePass(this.userId,this.passForm)
          if(res.data.flag){
            this.$message("修改密码成功")
            this.logout()
          }
        }
      })
    },
    //退出登录方法
    logout(){
      this.$store.dispatch("clearLogout").then(response=>{
        if(response.data.flag){
          this.$message("退出成功")
           //3. 跳转到登录页
          this.$router.push("/login")
        }
      })
    }
  },
  components: {},
};
</script>

<style scoped>
.header-logo {
  vertical-align: middle;
  padding-right: 10px;
}
.header-title {
  color: #fff;
  vertical-align: middle;
  font-size: 16px;
}
.el-dropdown{
    float: right;
    color : #fff;
}
</style>
